<?php
namespace Common\Model;

//微信接口
class WxApiModel{
    
    /**
     * 网页授权获取用户信息-获取code
     * @param $redirectUri string 回调链接地址
     */
    public function getAuthCode($redirectUri = '',$appid){
        $redirectUri=urlencode($redirectUri);
        header("Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirectUri."&response_type=code&scope=snsapi_base&state=123#wechat_redirect");die;
    }
    
    /**
     * 网页授权获取用户信息-code换取网页授权access_token
     * @param $code string
     * @return 用户openid
     */
    public function getAuthOpenid($wx_code = '',$appid,$secret){
        if(empty($wx_code)) return false;
        $wx_user_json=file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$wx_code."&grant_type=authorization_code");
        $wx_res=json_decode($wx_user_json,true);
        return $wx_res['openid'];
    }
    
}